package com.maizhe.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.maizhe.entity.SysLog;
import com.maizhe.entity.User;
import com.maizhe.entity.vo.UserVo;
import com.maizhe.service.SysLogService;
import com.maizhe.service.UserService;
import com.maizhe.util.BaseUtil;
import com.maizhe.util.JsonResult;
import com.maizhe.util.SsoUtil;
import com.ultrapower.casp.common.datatran.data.user.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.security.auth.Subject;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
	//protected Logger logger = LogManager.getLogger(getClass());
	private Logger logger=Logger.getLogger(LoginController.class);
	@Autowired
	private UserService userService;
	@Autowired
	private SysLogService sysLogService;

    @Resource
    private SsoUtil ssoUtil;
	
	@RequestMapping(value="/user/login")
	@ResponseBody
	public JsonResult<User> login(User user, HttpServletRequest request){
		//HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		JsonResult<User> result=userService.login(user);
		if(result.getState()==0){
			UserVo userVo=userService.findById(result.getData().getId());
			request.getSession().setAttribute("user",userVo);
			request.getSession().setAttribute("loginName",userVo.getLoginName());
			//request.getSession().setAttribute("roles",userVo.getRoles());
			//request.getSession().setAttribute("");
			logger.warn(userVo.getLoginName()+"【"+userVo.getRoles()+"】登陆成功");
			SysLog sysLog = new SysLog();
			sysLog.setMethod("login");
			sysLog.setMsg(userVo.getLoginName()+"【"+userVo.getRoles()+"】登陆成功");
			sysLog.setLocation("LoginController");
			sysLogService.saveLog(sysLog);
		}else{
			logger.warn(user.getLoginName()+"【"+user.getPassword()+"】登陆失败,"+result.getMessage());

			SysLog sysLog = new SysLog();
			sysLog.setMethod("login");
			sysLog.setMsg(user.getLoginName()+"【"+user.getPassword()+"】登陆失败,"+result.getMessage());
			sysLog.setLocation("LoginController");
			sysLogService.saveLog(sysLog);
		}
		return result;
	}

    @RequestMapping(value="/bonc/oa/singleLoginFor4A")
    public ModelAndView loginFor4A(HttpServletRequest request, HttpServletResponse response){
        ModelAndView mv = new ModelAndView();

        try {
            ssoUtil.doPost(request, response);
            UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
			//UserInfo userInfo = new UserInfo() ;
			//userInfo.setAccountID("admin");
			logger.info("单点登录用户信息:"+JSON.toJSONString(userInfo));
			if (userInfo != null && StringUtils.isNotBlank(userInfo.getAccountID())) { //登录成功，跳转首页
                UserVo userVo = userService.findByLoginName(userInfo.getAccountID());

                if (userVo != null) {
                    request.getSession().setAttribute("user",userVo);
                    request.getSession().setAttribute("loginName",userVo.getLoginName());

                    logger.warn(userVo.getLoginName()+"【"+userVo.getRoles()+"】登陆成功");
                    SysLog sysLog = new SysLog();
                    sysLog.setMethod("login");
                    sysLog.setMsg(userVo.getLoginName()+"【"+userVo.getRoles()+"】登陆成功");
                    sysLog.setLocation("LoginController");
                    sysLogService.saveLog(sysLog);

                    mv.setViewName("redirect:/toIndex"); //首页
                    return mv;

                }
            }

        } catch (Exception e) {
            logger.error("4A跳转出错！", e);
        }

        mv.setViewName("redirect:/toLogin"); //错误提示页面
        return mv;
    }

	/**
	 * 退出
	 * @return {Result}
	 */
	/*@RequestMapping("/logout")
	@ResponseBody
	public Object logout() {
		logger.info("登出");
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return renderSuccess();
	}*/

}
